package com.cwz.seata.demo1.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwz.api.entity.SeataDemo;
import com.cwz.seata.demo1.mapper.SeataDemoMapper;
import com.cwz.seata.demo1.service.SeataDemoService;
import io.seata.core.context.RootContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Wen
 * @since 2023-10-08
 */
@Service
public class SeataDemoServiceImpl extends ServiceImpl<SeataDemoMapper, SeataDemo> implements SeataDemoService {
	@Autowired
	private SeataDemoMapper seataDemoMapper;

	/**
	 * 开启 seata 事务传播特性设置为 REQUIRES_NEW 开启新的事务 重要！！！！一定要使用 REQUIRES_NEW
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public int updateNum(Long id) {
		System.out.println(RootContext.getXID());
		SeataDemo seataDemo = new SeataDemo();
		seataDemo.setId(1L);
		seataDemo.setNum(8);
		return this.seataDemoMapper.updateById(seataDemo);
	}

	/**
	 * 开启 seata 事务传播特性设置为 REQUIRES_NEW 开启新的事务 重要！！！！一定要使用 REQUIRES_NEW
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public int updatePrice(Long id) {
		System.out.println(RootContext.getXID());
		SeataDemo seataDemo = new SeataDemo();
		seataDemo.setId(1L);
		seataDemo.setPrice(new BigDecimal("8"));
		return this.seataDemoMapper.updateById(seataDemo);
	}

	/**
	 * 开启 seata 事务传播特性设置为 REQUIRES_NEW 开启新的事务 重要！！！！一定要使用 REQUIRES_NEW
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public int addSeataDemo() {
		System.out.println(RootContext.getXID());
		SeataDemo seataDemo = new SeataDemo();
		seataDemo.setNum(20);
		seataDemo.setPrice(new BigDecimal("20"));
		return this.seataDemoMapper.insert(seataDemo);
	}
}
